<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/28 15:39
 * Desc: 线路产品
 */
class Controller_Pc_Api_V2_Line extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Line
     */
    private $validation;

    /**
     * 线路服务
     * @var Service_Api_Line
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('line', $this->request_params);

        // 服务
        $this->service = new Service_Api_Line();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    // 查询条件
    public function action_get_query_condition()
    {
        try {
            $result = $this->service->query_condition();

            $this->result['data'] = $result;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 线路搜索
    public function action_general_query()
    {
        try {
            $this->validation->general_query()->check();

            $params = array(
                'page' => Arr::get($this->request_params, 'page', 1),
                'pagesize' => Arr::get($this->request_params, 'pagesize', 10),
                'keyword' => Arr::get($this->request_params, 'keyword', ''),
                'dest_pinyin' => Arr::get($this->request_params, 'dest_py', ''),
                'day_id' => Arr::get($this->request_params, 'day_id', 0),
                'price_id' => Arr::get($this->request_params, 'price_id', 0),
                'sort_type' => Arr::get($this->request_params, 'sort_type', 0),
                'start_city_id' => Arr::get($this->request_params, 'start_city_id', 0),
                'attr_id' => Arr::get($this->request_params, 'attr_id', 0),
                'mid' => $this->mid
            );

            $result               = $this->service->search($params);
            $this->result['data'] = $result;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 线路详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $params['id']     = $this->request_params['id'];
            $params['userid'] = $this->mid;

            $data = $this->service->info($params);

            if (empty($data)) {
                throw new Exception('未找到线路信息');
            }

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取合同内容
    public function action_contract()
    {
        try {
            $this->validation->contract()->check();

            $id = $this->request_params['id'];

            $data = $this->service->get_contract($id);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['msg'] = $e->getMessage();
        }
    }

    // 获取可报名人群
    public function action_get_people_group()
    {
        $this->result['data'] = $this->service->get_people_group();
    }

    // 创建订单
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $order_info = $this->service->create_order($this->request_params);

            $this->result['msg']  = '订单创建成功';
            $this->result['data'] = $order_info;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 首页推荐
    public function action_get_index_recommend()
    {
        try {
            $this->validation->get_index_recommend()->check();

            $pagesize = Arr::get($this->request_params, 'pagesize', 4);
            $page     = Arr::get($this->request_params, 'page', 1);

            $this->result['data'] = $this->service->get_recommend($pagesize, $page);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 根据目的地获取线路
    public function action_get_dest_list()
    {
        try {
            $this->validation->get_dest_list()->check();

            $dest_id  = $this->request_data->dest_id;
            $page     = $this->request_data->page;
            $pagesize = $this->request_data->pagesize;

            $data                 = $this->service->get_list_by_destination($dest_id, $page, $pagesize);
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取线路套餐
    public function action_get_suit_list()
    {
        try {

            $id      = $this->request_data->line_id;
            $suit_id = Arr::get($this->request_params, 'suit_id', '');
            // 秒杀
            $is_seckill = Arr::get($this->request_params, 'is_seckill', '');
            // 卡套餐
            $card_suit_id = Arr::get($this->request_params, 'card_suit_id', '');
            // 拼团
            $together_id = Arr::get($this->request_params, 'together_id', '');

            if($is_seckill && empty($suit_id)){
                throw new Exception('suit_id不能为空');
            }

            $params = array(
                'is_seckill' => $is_seckill,
                'card_suit_id' => $card_suit_id,
                'together_id' => $together_id,
                'suit_id' => $suit_id,
                'mid' => $this->mid
            );
            $data   = $this->service->get_suit_list($id, $params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取报价日历html新版
    public function action_get_price_calendar_json()
    {
        try {
            $this->validation->get_price_calendar_json()->check();

            $suit_id     = Arr::get($this->request_params, 'suit_id');
            $product_id  = Arr::get($this->request_params, 'product_id', '');
            $start_date  = Arr::get($this->request_params, 'date', '');
            $year        = Arr::get($this->request_params, 'year', '');
            $month       = Arr::get($this->request_params, 'month', '');
            $is_seckill  = Arr::get($this->request_params, 'is_seckill', '');
            $together_id = Arr::get($this->request_params, 'together_id', '');

            $params = array(
                'suit_id' => $suit_id,
                'product_id' => $product_id,
                'start_date' => $start_date,
                'year' => $year,
                'month' => $month,
                'is_seckill' => $is_seckill,
                'together_id' => $together_id
            );

            $data                 = $this->service->get_price_calendar($params);
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}